﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using GD.ModelContest.Common;
using GD.ModelContest.Model;
using GD.ModelContest.Service;
using System.IO;

namespace GD.ModelContest.Web
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSubmit_Click1(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(this.txtEmail.Text.Trim()) && !String.IsNullOrEmpty(this.txtRePassword.Text.Trim()) && !String.IsNullOrEmpty(this.txtName.Text.Trim()) && !String.IsNullOrEmpty(this.txtIDCard.Text.Trim()) && !String.IsNullOrEmpty(this.txtNation.Text.Trim()) && !String.IsNullOrEmpty(this.txtSpecialty.Text.Trim()) && !String.IsNullOrEmpty(this.txtPhoneCode.Text.Trim()) && !String.IsNullOrEmpty(this.txtAddress.Text.Trim()) && !String.IsNullOrEmpty(this.txtPostcode.Text.Trim()))
            {
                mc_user mcUser = new mc_user();

                mcUser.email = this.txtEmail.Text.Trim();
                mcUser.password = MD5.MD5_32(this.txtRePassword.Text.Trim());
                mcUser.items = this.rblItems.SelectedValue.ToString();
                mcUser.province = Request["Province"]; //this.Province.SelectedValue.ToString();
                mcUser.city = Request["City"];//this.City.SelectedValue.ToString();
                mcUser.district = Request["District"];//this.District.SelectedValue.ToString();
                mcUser.name = this.txtName.Text.Trim();
                mcUser.secName = this.txtSecName.Text.Trim();
                mcUser.birthday = Convert.ToDateTime(this.txtBirthday.Text.Trim()); 
                mcUser.sex = this.rblSex.SelectedValue.ToString();
                mcUser.isMarry = this.rblMarry.SelectedValue.ToString();
                mcUser.idCard = this.txtIDCard.Text.Trim();
                mcUser.nation = this.txtNation.Text.Trim();
                mcUser.bloodType = this.rblBloodType.SelectedValue.Trim();
                mcUser.bodyHeight = this.ddlBodyHeight.SelectedValue.ToString();
                mcUser.bodyWeight = this.ddlBodyWeight.SelectedValue.ToString();
                mcUser.shoeSize = this.ddlShoeSize.SelectedValue.ToString();
                mcUser.bust = this.ddlBust.SelectedValue.ToString();
                mcUser.waist = this.ddlWaist.SelectedValue.ToString();
                mcUser.hip = this.ddlHip.SelectedValue.ToString();
                mcUser.shoulderWidth = this.ddlShoulderWidth.SelectedValue.ToString();
                mcUser.education = this.ddlEducation.SelectedValue.ToString();
                mcUser.specialty = this.txtSpecialty.Text.Trim();
                mcUser.religion = this.txtReligion.Text.Trim();
                mcUser.health = this.rblHealth.SelectedValue.ToString();
                mcUser.phoneCode = this.txtPhoneCode.Text.Trim();
                mcUser.address = this.txtAddress.Text.Trim();
                mcUser.postCode = this.txtPostcode.Text.Trim();
                mcUser.school = this.txtSchool.Text.Trim();
                mcUser.intro = this.txtIntro.Text.Trim();
                mcUser.createTime = DateTime.Now;
                mcUser.lastLoginTime = DateTime.Now;
                mcUser.pinCode = "00000000";
                if (ServiceLocator.IUserService.InsertUser(mcUser))
                {
                    //上传彩信图片
                    try
                    {
                        if (fuPhoto.HasFile)
                        {
                            //判断图片格式是否正确
                            string photoExt = Path.GetExtension(this.fuPhoto.FileName).ToLower();
                            if (photoExt == ".gif" || photoExt == ".jpg")
                            {
                                string name = this.fuPhoto.PostedFile.FileName; //文件路径
                                FileInfo fileInfo = new FileInfo(name);

                                Random rd = new Random();
                                string fileName = "MC" + mcUser.userId + DateTime.Now.ToString("yyyyMMddmmhhss") + rd.Next(10, 99) + photoExt;   //文件名称
                                string sFileName = "x_" + fileName;             //缩略图文件名 

                                string filePath = Server.MapPath("~/ImgUpLoad/" + fileName);    //服务器端文件路径
                                string sFilePath = Server.MapPath("~/ImgUpLoad/" + sFileName);  //缩略图文件路径

                                int w = 178;
                                int h = 203;

                                this.fuPhoto.SaveAs(filePath);
                                ThumbNail.MakeThumNail(filePath, sFilePath, w, h, "HW");
                                mc_photo mcPhoto = new mc_photo();
                                mcPhoto.photoName = fileName;
                                mcPhoto.thumbnailName = sFileName;
                                mcPhoto.isHeadPhoto = true;
                                mcPhoto.uploadTime = DateTime.Now;
                                mcPhoto.userId = mcUser.userId;
                                bool upPhoto = ServiceLocator.IUserService.InsertPhoto(mcPhoto);
                            }
                            else
                            {
                                JavaScript.WriteJs("jAlert('上传的图片格式不对','警告',null)", this.Page);
                                return;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        JavaScript.WriteJs("jAlert('" + ex.ToString() + "','警告',null)", this.Page);
                        return;
                    }
                    mc_user user = ServiceLocator.IUserService.GetUserById(mcUser.userId);
                    string codePrefix = this.rblSex.SelectedValue.ToString() == "男" ? "MC01" : "MC02";
                    string codeSuffix = mcUser.userId.ToString().PadLeft(5, '0');
                    user.pinCode = codePrefix + codeSuffix;
                    bool upUser = ServiceLocator.IUserService.UpdateUser(user);
                    mc_vote vote = new mc_vote();
                    vote.userId = mcUser.userId;
                    vote.voteNum = 0;
                    bool upVote = ServiceLocator.IUserService.InsertVote(vote);
                    JavaScript.ShowJS("恭喜您，报名成功！","Index.aspx", this.Page);
                }
            }
            else
            {
                JavaScript.Alert("有未填的内容，请仔细检查表单，*号为必填内容。", this.Page);
            }
        }
    }
}
